/**
 * @author lianPf
 * @date 17-12-12
 * */
import React from 'react'
import moment from 'moment'
import { Table, message } from 'antd'
import Search from '../../common/components/Search'
import searchStyle from '../../common/less/search.less'
import { fetchPost } from '../../../utils/request'

class Index extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      list: [],
      params: {
        startDate: moment().subtract('days', 7).format('YYYY-MM-DD'),
        endDate: moment().add('days', 0).format('YYYY-MM-DD'),
        acceptNo: '',
        queryItemType: '',
        queryItemValue: ''
      },
      page: {
        currentPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      searchParams: {
        list: [
          {
            name: '还款时间',
            type: 'range',
            key: ['startDate', 'endDate'],
            className: 'pr20',
            placeHolder: '',
            values: [
              moment().subtract('days', 7),
              moment().add('days', 0)
            ]
          },
          {
            name: '商户流水号',
            type: 'text',
            key: 'acceptNo',
            className: 'pr20',
            placeHolder: '请输入商户流水号',
            values: []
          },
          {
            name: '个人信息',
            type: 'group',
            key: ['queryItemType', 'queryItemValue'],
            className: 'pr20',
            values: [{value: '', name: '请选择'}]
          },
          {
            name: '',
            type: 'searchBtn',
            key: '',
            className: 'pr20',
            value: []
          }
        ],
        api: '/settle/fund/query'
      }
    }
  }

  componentDidMount () {
    this.getData()
  }

  getData () {
    let self = this
    const { page, params } = this.state
    const { currentPage, pageSize } = page
    let copyState = {}
    for (let i in params) {
      if (params[i] !== '') {
        copyState[i] = params[i]
      }
    }
    fetchPost(`/settle/fund/getRepeatRepayment?currentPage=${currentPage}&pageSize=${pageSize}`, copyState).then((res) => {
      if (res.code === 0) {
        const { data, page } = res.data
        const { currentPage, pageSize, totalCount } = page
        self.setState({
          list: data,
          page: {
            currentPage,
            pageSize,
            totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    })
  }

  nextpage (current) {
    this.setState({
      page: {
        ...this.state.page,
        currentPage: current
      }
    }, () => {
      this.getData()
    })
  }

  changeParams (params) {
    this.setState({
      params
    })
  }

  searchFunc (params) {
    this.setState({
      params
    }, () => {
      this.getData()
    })
  }

  render () {
    const self = this
    const columns = [{
      title: '商户流水号',
      dataIndex: 'acceptNo',
      key: 'acceptNo'
    },
    {
      title: '产品',
      key: 'paymentSysName',
      dataIndex: 'paymentSysName'
    },
    {
      title: '资金方',
      key: 'fundName',
      dataIndex: 'fundName'
    },
    {
      title: '还款渠道',
      key: 'paymentChannelName',
      dataIndex: 'paymentChannelName'
    },
    {
      title: '姓名',
      key: 'acctName',
      dataIndex: 'acctName'
    },
    {
      title: '手机号',
      key: 'mobile',
      dataIndex: 'mobile'
    },
    {
      title: '银行卡号',
      key: 'cardNo',
      dataIndex: 'cardNo'
    },
    {
      title: '还款时间',
      key: 'repayDate',
      dataIndex: 'repayDate'
    },
    {
      title: '还款金额',
      key: 'repayAmount',
      dataIndex: 'repayAmount'
    }
    ]

    const pagination = {
      total: this.state.page.totalCount || 0,
      pageSize: this.state.page.pageSize,
      showSizeChanger: false,
      showQuickJumper: true,
      showTotal (total) {
        return `Total ${total} `
      },
      onChange (current) {
        self.nextpage(current)
      }
    }

    return (
      <div>
        <div className={searchStyle.searchClass}>
          <Search searchParams={this.state.searchParams} changeParams={(params) => this.changeParams(params)} searchFunc={(params) => this.searchFunc(params)} />
        </div>

        <Table pagination={pagination} key='name' columns={columns} dataSource={this.state.list} loading={this.state.loading}
          bordered
         />
      </div>
    )
  }
}

export default Index
